﻿/**
 * ...
 * @author Alex Malygin
 */

package list;

import flash.text.TextFormat;
import gui.hug.HUGButton;
import gui.hug.HUGSprite;
import flash.events.MouseEvent;
import haxe.Timer;
import loader.Loader;
import client.Item;
import client.ReceivedItem;



class FriendsPanel  extends HUGSprite
{
	private var addFriends		: HUGButton;
	private var mail	    	: HUGButton;
	private var mCount 			: Int;
	private var tm 				: Timer;
	private static var loader  	: Loader;
	
	public function new(left : UInt = 0, top : UInt = 0, onClick: Void -> Void, onMailClick: Array<ReceivedItem> -> Void, count : Int ) {
		super();
		mCount = count;
		setLocation(left, top);
		Loader.createInstance();
		loader = Loader.getInstance();
		
		addFriends = new HUGButton("Добавить друзей");
		addFriends.setLocation(left - 368 , top + 10);
		addFriends.addEventListener(MouseEvent.CLICK, function(e) { onClick(); } );
				
		mail = new HUGButton("Почта ("+mCount+")");
		mail.setLocation(left - 220, top + 10);
		mail.addEventListener(MouseEvent.CLICK, function (e) 
		{
			loader.readMessages(onMailClick);
		});
		addChild(addFriends);
		addChild(mail);
		tm = new Timer(3000);
	}
	
	private function refresh() {
		loader.messagesToRead(changeCount);
	}
	
	private function changeCount(c : Int) {
		mail.setText("Почта(" + c + ")");
	}
	
	public function startTimer() {
		tm.run = refresh;
	}
}